package com.lry.event;

import com.lry.bean.ExceptionNotice;
import com.lry.send.INoticeSendComponent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;

import java.time.LocalDateTime;
import java.util.List;
@Slf4j
public abstract class AbstractNoticeSendListener implements ApplicationListener<ExceptionNoticeEvent>  {

    private List<INoticeSendComponent<ExceptionNotice>> sendComponents;

    public AbstractNoticeSendListener( List<INoticeSendComponent<ExceptionNotice>> sendComponents) {
        this.sendComponents = sendComponents;
    }

    public void send(ExceptionNotice notice){

        notice.setShowCount(1L);
        notice.setCreateTime(LocalDateTime.now());
        sendComponents.forEach(sendComponent->{
            try{
                sendComponent.send(notice);
            }catch (Exception e){
                e.printStackTrace();
                log.error(e.getMessage());
            }
        });

    }

}
